<?php


namespace app\api\model\dao;


use app\library\Flag;
use think\Log;
use think\Model;

class UserCurrencyExchange extends Model
{
    protected $table = 'user_currency_exchange';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    /**
     * @param $data
     * @return array|false
     * @throws \Exception
     */
    public function addExchangeRecord($data)
    {
        return $this->allowField(true)->saveAll($data);
    }

    public function exchangePage()
    {
        return $this->paginate(10);
    }

    /**
     * 用户消费漫币漫豆记录
     * @param array $conds
     * @param array $fields
     * @param int $offset
     * @param int $limit
     * @param string $orderby
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getExchangeRecord($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);
        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;
        return $this->where($arrCond)->limit($offset, $limit)->order($orderby, 'DESC')->field($arrField)->select();
    }

    public function getExchangeTypeAttr($value)
    {
        return Flag::$currency_exchange_map[$value];
    }

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d h:i', $value);
    }
}